package com.travel.travelServer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelServer.entity.Car;
import com.travel.travelServer.mapper.CarMapper;
import com.travel.travelServer.mapper.GoodsMapper;
import com.travel.travelServer.mapper.UserMapper;
import com.travel.travelServer.service.CarService;
import com.travel.travelServer.utils.BaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (Car)表服务实现类
 *
 * @author makejava
 * @since 2024-03-01 15:38:34
 */
@Service("carService")
public class CarServiceImpl extends BaseService<CarMapper, Car> implements CarService {

    @Resource
    private GoodsMapper fruitDao;

    @Resource
    private UserMapper userDao;

    @Resource
    private CarMapper carDao;

    @Override
    public Page<Car> page(Page<Car> page, Map<String, Object> conditions) {
        Long userId = getCurrentUser().getId();
        LambdaQueryWrapper<Car> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Car::getStatus, 0);
        wrapper.eq(Car::getUserId, userId);
        Page<Car> carPage = super.page(page, wrapper);
        carPage.getRecords().stream().forEach(car -> {
            if (Objects.nonNull(car.getFruitId())){
                car.setGoods(fruitDao.selectById(car.getFruitId()));
            }
            if (Objects.nonNull(car.getUserId())){
                car.setUser(userDao.selectById(car.getUserId()));
            }
        });
        return carPage;
    }

    @Override
    public Car getById(Long id) {
        Car car = super.getById(id);
        if (Objects.nonNull(car.getFruitId())){
            car.setGoods(fruitDao.selectById(car.getFruitId()));
        }
        if (Objects.nonNull(car.getUserId())){
            car.setUser(userDao.selectById(car.getUserId()));
        }
        return car;
    }

    @Override
    public Boolean add(Car pojo) {
        Long userId = getCurrentUser().getId();
        pojo.setUserId(userId);
        LambdaQueryWrapper<Car> carLambdaQueryWrapper = new LambdaQueryWrapper<>();
        carLambdaQueryWrapper.eq(Car::getUserId, userId);
        carLambdaQueryWrapper.eq(Car::getStatus, 0);
        carLambdaQueryWrapper.eq(Car::getFruitId, pojo.getFruitId());
        Car car = super.getOne(carLambdaQueryWrapper);
        if (Objects.nonNull(car)){
            car.setNumber(pojo.getNumber());
            return super.updateById(car);
        }else {
            return super.save(pojo);
        }
    }

    @Override
    public Boolean update(Car pojo) {
        return super.updateById(pojo);
    }

    @Override
    public Boolean removeOne(Long id) {
        return super.removeById(id);
    }

    @Override
    public Boolean batchRemove(List<Long> asList) {
        return super.removeByIds(asList);
    }

    @Override
    public List<Car> getMyCar() {
        Long userId = getCurrentUser().getId();
        LambdaQueryWrapper<Car> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Car::getStatus, 0);
        wrapper.eq(Car::getUserId, userId);
        List<Car> cars = carDao.selectList(wrapper);
        cars.stream().forEach(car -> {
            if (Objects.nonNull(car.getFruitId())){
                car.setGoods(fruitDao.selectById(car.getFruitId()));
            }
        });
        return cars;
    }
}

